package com.example.villagemanager;

import android.annotation.SuppressLint;
import android.app.Application;

import com.blankj.utilcode.util.CrashUtils;
import com.blankj.utilcode.util.Utils;
import com.example.villagemanager.model.response.UserLoginResp;
import com.example.villagemanager.network.RetrofitHelper;
import com.example.villagemanager.network.UrlDefine;
import com.example.villagemanager.ui.widget.ToastTool;
import com.example.villagemanager.util.AppStarter;
import com.example.villagemanager.util.DeviceRelatedUtil;
import com.example.villagemanager.util.WelcomeDialogController;
import com.example.villagemanager.util.bluetooth.BlueToothUtil;
import com.example.villagemanager.util.starttask.GetConfigStartTaskImpl;
import com.example.villagemanager.util.starttask.ShowAdsStartTaskImpl;
import com.example.villagemanager.util.starttask.TimeOutTask;
import com.liulishuo.filedownloader.FileDownloader;

import org.json.JSONObject;
import org.lzh.framework.updatepluginlib.UpdateConfig;
import org.lzh.framework.updatepluginlib.base.CheckCallback;
import org.lzh.framework.updatepluginlib.base.DownloadCallback;
import org.lzh.framework.updatepluginlib.base.UpdateParser;
import org.lzh.framework.updatepluginlib.base.UpdateStrategy;
import org.lzh.framework.updatepluginlib.model.Update;

import java.io.File;

public class VillageManagerApp extends Application {
    public static UserLoginResp userLoginResp;
    public static VillageManagerApp intance;
    public static BlueToothUtil blueToothUtil;
    @SuppressLint("MissingPermission")
    @Override
    public void onCreate() {
        super.onCreate();
        DeviceRelatedUtil.init(this);
        blueToothUtil= BlueToothUtil.init(this);
        intance=this;
        addStartTask();
        Utils.init(this);
        CrashUtils.init();
        //Retrofit
        RetrofitHelper.init(this);
        ToastTool.init(this);
        //初始化更新插件
        initUpdateConfig();
        //文件下载插件初始化
        FileDownloader.setupOnApplicationOnCreate(this);
    }

    // 添加启动任务到AppStarter里
    private void addStartTask() {
        AppStarter.getInstance().add(new ShowAdsStartTaskImpl());
        AppStarter.getInstance().add(new GetConfigStartTaskImpl());
        AppStarter.getInstance().add(new TimeOutTask(AppStarter.getInstance()));

        AppStarter.getInstance().start(WelcomeDialogController.getInstance().getListener());  // 执行启动任务
    }
    private void initUpdateConfig(){
        String url= UrlDefine.BASE_URL+ UrlDefine.APPUPDATE;
        System.err.println("url="+url);
        UpdateConfig.getConfig()
                .setUrl(url)// 配置检查更新的API接口
                .setUpdateParser(new UpdateParser() {
                    @Override
                    public Update parse(String response) throws Exception {
                        System.err.println("UpdateParser.response="+response);
                        JSONObject object = new JSONObject(response);
                        Update update = new Update();
                        // 此apk包的下载地址
                        update.setUpdateUrl(object.optString("path"));
                        // 此apk包的版本号
                        update.setVersionCode(Integer.parseInt(object.optString("versionCode")));
                        // 此apk包的版本名称
                        update.setVersionName(object.optString("versionName"));
                        // 此apk包的更新内容
                        update.setUpdateContent("");
                        // 此apk包是否为强制更新
                        update.setForced(true);
                        // 是否显示忽略此次版本更新按钮
                        update.setIgnore(object.optBoolean("ignore_able",false));
                        update.setMd5(object.optString("md5"));
                        return update;
                    }


                })      .setCheckCallback(new CheckCallback() {
                    @Override
                    public void onCheckStart() {

                    }
                    @Override
                    public void hasUpdate(Update update) {

                    }
                    @Override
                    public void noUpdate() {
                        ToastTool.show(getString(R.string.no_update));
                    }
                    @Override
                    public void onCheckError(Throwable t) {
                        ToastTool.show(getString(R.string.update_failed));
                    }
                    @Override
                    public void onUserCancel() {
                        ToastTool.show(getString(R.string.update_canceled));
                    }
                    @Override
                    public void onCheckIgnore(Update update) {
                        ToastTool.show(getString(R.string.update_ignored));
                    }                })
                .setUpdateStrategy(new UpdateStrategy() {
                    @Override
                    public boolean isShowUpdateDialog(Update update) {
                        //是否提示更新
                        return true;
                    }
                    @Override
                    public boolean isShowDownloadDialog() {
                        //是否显示下载进度
                        return true;
                    }
                    @Override
                    public boolean isAutoInstall() {
                        // 下载完成后是否直接安装
                        return true;
                    }
                })
                .setDownloadCallback(new DownloadCallback() {
                    @Override
                    public void onDownloadStart() {

                    }
                    @Override
                    public void onDownloadComplete(File file) {
                    }
                    @Override
                    public void onDownloadProgress(long current, long total) {

                    }
                    @Override
                    public void onDownloadError(Throwable t) {
                        ToastTool.show(getString(R.string.download_failed));
                    }
                });
    }
}

